package com.dengyuanke.spring.cloud.weather.job;

import com.dengyuanke.spring.cloud.weather.service.CityDataService;
import com.dengyuanke.spring.cloud.weather.service.WeatherDataService;
import com.dengyuanke.spring.cloud.weather.vo.City;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Author: dyk
 * 天气数据同步服务
 * Create time: 2018/9/6  10:48
 */
public class WeatherDataSyncJob extends QuartzJobBean {
    private final static Logger LOGGER= LoggerFactory.getLogger(WeatherDataSyncJob.class);

    @Autowired
    private CityDataService cityDataService;
    @Autowired
    private WeatherDataService weatherDataService;

    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        LOGGER.info("Start天气数据同步任务");
        List<City> cityList=null;
        try {
        cityList=cityDataService.listCity();
        }catch (Exception e){
            LOGGER.info("获取城市信息异常！",e);
        }
        for (City city:cityList){
            String cityId=city.getCityId();
            LOGGER.info("天气数据同步任务中，cityId:"+cityId);
            weatherDataService.syncDataByCityId(cityId);
        }
        LOGGER.info("End 天气数据同步任务");
    }
}
